package com.sjsu.cloudmanager.client.ui;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.*;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.util.Margins;

import com.extjs.gxt.ui.client.widget.*;
import com.extjs.gxt.ui.client.widget.layout.*;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Widget;
import com.sjsu.cloudmanager.client.ui.model.LabTreeBean;

import java.util.List;


/**
 * Lab main panel
 * <p/>
 * User: ckempaiah
 * Date: 9/22/11
 * Time: 11:59 PM
 * To change this template use File | Settings | File Templates.
 */
public class LabMainPanel extends LayoutContainer {
    private TreePanel treePanel;

    @Override
    protected void onRender(Element parent, int index) {
        super.onRender(parent, index);
        setBorders(true);

//        TabPanel managerAppPanel = new TabPanel();
//        managerAppPanel.setAutoWidth(true);
//        managerAppPanel.setAutoHeight(true);
//        managerAppPanel.setResizeTabs(true);
//        TabItem manageLab = new TabItem("Manage Labs");
//        //manageLab.addStyleName("pad-text");
//        managerAppPanel.add(manageLab);
//        manageLab.setBorders(false);

        setLayout(new BorderLayout());
        setHeight(650);


        BorderLayoutData northData = new BorderLayoutData(Style.LayoutRegion.NORTH, 25);
        northData.setMargins(new Margins(10, 5, 0, 5));

        Text text = new Text();
        text.setText("<div style='font-size: 14px; font-family: Verdana; font-weight:bold'>Cloud Lab Manager Application</div>");


        //text.setStyleAttribute();

        //text.setStyleAttribute("font", "18px");

        BorderLayoutData westData = new BorderLayoutData(Style.LayoutRegion.WEST, 200);
        westData.setSplit(true);
        westData.setCollapsible(true);
        westData.setMargins(new Margins(5, 0, 5, 5));
        ContentPanel westPanel = getTreePanel();

        final ContentPanel centerPanel = new ContentPanel();
        BorderLayoutData centerData = new BorderLayoutData(Style.LayoutRegion.CENTER);
        centerData.setMargins(new Margins(5));
        centerPanel.setButtonAlign(Style.HorizontalAlignment.CENTER);
        centerPanel.setHeaderVisible(false);

        add(text, northData);
        add(centerPanel, centerData);
        add(westPanel, westData);


//        manageLab.add(cp);
        treePanel.addListener(Events.OnClick, new Listener<TreePanelEvent<ModelData>>() {
            @Override
            public void handleEvent(TreePanelEvent<ModelData> be) {

                if (be.getItem().get("name").equals("Labs")) {
                    //MessageBox.alert("Selected Item", be.getItem().get("name").toString(), null);
                    final LabGrid labGrid = new LabGrid();
                    centerPanel.remove(Registry.<Widget>get("centerData"));
                    Registry.register("centerData", labGrid);
                    centerPanel.add(labGrid, new FitData());
                    labGrid.show();
                    centerPanel.layout();
                    //labGrid.getGridStore().getLoader().load();
                } else if (be.getItem().get("name").equals("Machines")) {
                    LabResourceMainPanel labResourceMainPanel = new LabResourceMainPanel();
                    centerPanel.remove(Registry.<Widget>get("centerData"));
                    Registry.register("centerData", labResourceMainPanel);
                    centerPanel.add(labResourceMainPanel);
                    labResourceMainPanel.show();
                    centerPanel.layout();
                    //MessageBox.alert("Selected Item", be.getItem().get("name").toString(), null);
                } else if (be.getItem().get("name").equals("Resource Monitoring")) {
                    MonitoringMainPanel monitoringMainPanel = new MonitoringMainPanel();
                    centerPanel.remove(Registry.<Widget>get("centerData"));
                    Registry.register("centerData", monitoringMainPanel);
                    centerPanel.add(monitoringMainPanel);
                    monitoringMainPanel.show();
                    centerPanel.layout();
                    //MessageBox.alert("Selected Item", be.getItem().get("name").toString(), null);
                } else if (be.getItem().get("name").equals("Manage Users")){
                    LabUserMainPanel labUserMainPanel = new LabUserMainPanel();
                    centerPanel.remove(Registry.<Widget>get("centerData"));
                    Registry.register("centerData", labUserMainPanel);
                    centerPanel.add(labUserMainPanel);
                    labUserMainPanel.show();
                    centerPanel.layout();
                }
            }
        });

        if (treePanel.getSelectionModel().getSelectedItem() == null){
            List allitems  = treePanel.getStore().getAllItems();
            ModelData modelData = treePanel.getStore().getChild(0);
            treePanel.getSelectionModel().select(modelData, true);
            TreePanelEvent tpe = new TreePanelEvent<ModelData>(treePanel);
            tpe.setItem(modelData);
            treePanel.fireEvent(Events.OnClick, tpe);
        }

        //LabManagerLogin login = new LabManagerLogin();
        //add(login);
    }

    private ContentPanel getTreePanel() {


        TreeStore<ModelData> store = new TreeStore<ModelData>();
        store.add(getTreeModel().getChildren(), true);

        final TreePanel<ModelData> tree = new TreePanel<ModelData>(store);
        //tree.setWidth(200);
        tree.setDisplayProperty("name");
        this.treePanel = tree;
        //tree.getStyle().setLeafIcon(Resources.ICONS.music());

//        tree.addListener(Events.OnClick, new Listener<TreePanelEvent<ModelData>>() {
//            @Override
//            public void handleEvent(TreePanelEvent<ModelData> be) {
//                MessageBox.alert("Sample Item", be.getItem().toString(), null);
//                //To change body of implemented methods use File | Settings | File Templates.
//            }
//        });

//        tree.getSelectionModel().addSelectionChangedListener(new SelectionChangedListener<ModelData>() {
//            @Override
//            public void selectionChanged(SelectionChangedEvent<ModelData> modelDataSelectionChangedEvent) {
//
//                MessageBox.alert("Sample Item", modelDataSelectionChangedEvent.getSelectedItem().toString(), null);
//                //To change body of implemented methods use File | Settings | File Templates.
//            }
//        });

        ContentPanel treePanel = new ContentPanel();
        treePanel.setFrame(true);
        //treePanel.add(buttonBar, new FlowData(10));
        treePanel.add(tree, new FlowData(0));

        return treePanel;
    }

    public LabTreeBean getTreeModel() {

//        LabTreeBean[] treeBeanArray = new LabTreeBean[]{
//                new LabTreeBean(1, "lab1", new MachineTreeBean[]{
//                        new MachineTreeBean(2, "machine1"),
//                        new MachineTreeBean(3, "machine2")
//                }),
//                new LabTreeBean(4, "lab2", new MachineTreeBean[]{
//                        new MachineTreeBean(5, "machine1"),
//                        new MachineTreeBean(6, "machine2")
//                })
//        };
        LabTreeBean[] treeBeanArray = new LabTreeBean[]{
                new LabTreeBean(2, "Labs"),
                new LabTreeBean(3, "Machines"),
                new LabTreeBean(4, "Resource Monitoring"),
                new LabTreeBean(5, "Manage Users")
        };

        LabTreeBean root = new LabTreeBean(1, "root", treeBeanArray);

        return root;
    }
}
